package iceworld.fernado.file.impl;

import iceworld.fernado.consts.Constants;
import iceworld.fernado.entity.Field;
import iceworld.fernado.resource.SysConfig;
import iceworld.fernado.util.Utils;

import java.util.List;

public class CsharpDaoSaveResource extends SaveResource {

	private String firstLowerClazzName = Constants.EMPTY_STRING;
	@Override
	public String getLanguageSuffix() {
		return ".cs";
	}

	@Override
	public String generateLanuageFrame(String clazzName, String tableComment,
			List<Field> fields) {
		firstLowerClazzName = Utils.makeFirstCharacterLower(clazzName);
		return this.generateMainFrame(clazzName, tableComment, fields);
	}
	
	public String generateMainFrame(String clazzName, String tableComment,
			List<Field> fields) {
		StringBuilder result = new StringBuilder();
		result.append("using System;\r\n");
		result.append("using System.Collectons.Generic;\r\n");
		result.append("using System.Linq;\r\n");
		result.append("using System.Text;\r\n");
		result.append("using E3_FA.Core;\r\n");
		result.append("\r\n");
		result.append("namespace ");
		result.append(SysConfig.getInstance().getValue("dao_namespacename"));
		result.append("\r\n");
		result.append("{");
		result.append("\r\n");
		this.generateClass(result, clazzName, tableComment, fields);
		
		result.append("\r\n");
		result.append("}");
		return null;
	}
	
	private void generateClass(StringBuilder builder, String clazzName, String tableComment,
			List<Field> fields) {
		builder.append(Utils.howMany4Spaces(1));
		builder.append("public class ");
		builder.append(clazzName).append("DAO");
		builder.append(" ").append(Constants.COLON).append(" ");
		builder.append("BaseDAO, ");
		builder.append("I").append(clazzName).append("DAO");
		builder.append(Utils.howMany4Spaces(1));
		builder.append("\r\n");
		builder.append("{");
		
		this.generateClassContent(builder, clazzName, tableComment, fields);
		
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(1));
		builder.append("}");
	}
	
	private void generateClassContent(StringBuilder builder, String clazzName, String tableComment,
			List<Field> fields) {
		this.generateGetMethod(builder, clazzName, tableComment, fields);
		this.generateCreateMethod(builder, clazzName);
		this.generateUpdateMethod(builder, clazzName);
		this.generateDeleteMethod(builder, clazzName);
	}
	
	private void generateGetMethod(StringBuilder builder, String clazzName, String tableComment,
			List<Field> fields) {
		for (Field field: fields) {
			if (!Utils.isEmpty(field.getType())) {
				StringBuilder sb = new StringBuilder();
				sb.append(clazzName);
				sb.append("_");
				sb.append(field.getName());
				
				builder.append(Utils.howMany4Spaces(2));
				builder.append("public ");
				builder.append(clazzName);
				builder.append("Get");
				builder.append(sb);
				builder.append("(");
				builder.append(field.getType());
				builder.append(" ");
				builder.append(field.getName());
				builder.append(")");
				builder.append("\r\n");
				builder.append(Utils.howMany4Spaces(2));
				builder.append("{");
				builder.append(Utils.howMany4Spaces(3));
				builder.append("result ");
				builder.append("mapper.QueryForObject<");
				builder.append(clazzName);
				builder.append(">");
				builder.append("(");
				builder.append("\"get");
				builder.append(sb);
				builder.append("\"");
				builder.append(", ");
				builder.append(field.getName());
				builder.append(");");
				builder.append("\r\n");
				
				builder.append(Utils.howMany4Spaces(2));
				builder.append("}");	
			}
		}
	}
	
	private void generateCreateMethod(StringBuilder builder, String clazzName) {
		builder.append(Utils.howMany4Spaces(2));
		builder.append("public void Create");
		builder.append(clazzName);
		builder.append("(");
		builder.append(clazzName);
		builder.append(" ");
		builder.append(firstLowerClazzName);
		builder.append(")");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("{");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(3));
		builder.append("object obj");
		builder.append(" = ");
		builder.append("mapper.Insert(");
		builder.append("\"");
		builder.append("create");
		builder.append(clazzName);
		builder.append("\"");
		builder.append(", ");
		builder.append(firstLowerClazzName);
		builder.append(");");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("}");
		
	}
	
	private void generateUpdateMethod(StringBuilder builder, String clazzName) {
		builder.append(Utils.howMany4Spaces(2));
		builder.append("public void Update");
		builder.append(clazzName);
		builder.append("(");
		builder.append(clazzName);
		builder.append(" ");
		builder.append(firstLowerClazzName);
		builder.append(")");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("{");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(3));
		builder.append("object obj");
		builder.append(" = ");
		builder.append("mapper.Update(");
		builder.append("\"");
		builder.append("update");
		builder.append(clazzName);
		builder.append("\"");
		builder.append(", ");
		builder.append(firstLowerClazzName);
		builder.append(");");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("}");
	}

	private void generateDeleteMethod(StringBuilder builder, String clazzName) {
		builder.append(Utils.howMany4Spaces(2));
		builder.append("public void Delete");
		builder.append(clazzName);
		builder.append("(");
		builder.append(clazzName);
		builder.append(" ");
		builder.append(firstLowerClazzName);
		builder.append(")");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("{");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(3));
		builder.append("object obj");
		builder.append(" = ");
		builder.append("mapper.Delete(");
		builder.append("\"");
		builder.append("delete");
		builder.append(clazzName);
		builder.append("\"");
		builder.append(", ");
		builder.append(firstLowerClazzName);
		builder.append(");");
		builder.append("\r\n");
		builder.append(Utils.howMany4Spaces(2));
		builder.append("}");
	}
}
